using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace BAFIRCSharp
{
    public class IdentD
    {
        public string Ident = "";
        public string OS = "";
        public bool Active = false;
        public bool OneShot = false;
        public int ServedReqs = 0;
        private Thread IdentDT;

        public IdentD()
        {
            Ident = "";
            OS = "";
        }

        public IdentD(string Ident, string OS)
        {
            this.Ident = Ident;
            this.OS = OS;
        }

        public void StartIdentD()
        {
            if (Active)
                return;

            IdentDT = new Thread(new ThreadStart(IdentDTF));
            Active = true;
            IdentDT.Start();
        }

        public void StopIdentD()
        {
            Active = false;
        }

        private void IdentDTF()
        {
            Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            try
            {
                listener.Blocking = true;
                listener.Bind(new IPEndPoint(IPAddress.Any, 113));
                listener.Listen(3);
            }
            catch (SocketException se)
            {
                Console.WriteLine("SocketException: " + se.Message);
            }

            while (Active)
            {
                Socket NewConn = listener.Accept();
                while (NewConn.Available < 2)
                    Thread.Sleep(1);
                byte[] IdentReqData = new byte[NewConn.Available];
                string IdentReq;
                try
                {
                    NewConn.Receive(IdentReqData);
                    IdentReq = Encoding.ASCII.GetString(IdentReqData);
                    int POS = int.Parse(IdentReq.Substring(0, IdentReq.IndexOf(',')));
                    int POC;
                    if(IdentReq.IndexOf("\r") != -1 || IdentReq.IndexOf("\n") != -1)
                    {
                        int Pos = IdentReq.IndexOf("\r");
                        if((IdentReq.IndexOf("\n") < Pos && IdentReq.IndexOf("\n") != -1) || Pos == -1)
                            Pos = IdentReq.IndexOf("\n");
                        POC = int.Parse(IdentReq.Substring(IdentReq.IndexOf(',') + 2, Pos - (IdentReq.IndexOf(',') + 2)));
                    }
                    else
                        POC = int.Parse(IdentReq.Substring(IdentReq.IndexOf(',') + 2));

                    string Resp = POS + " , " + POC + " : USERID : " + OS + " : " + Ident;
                    NewConn.Send(Encoding.ASCII.GetBytes(Resp));
                    NewConn.Close();
                    if (OneShot)
                        Active = false;
                    ++ServedReqs;
                }
                catch (SocketException se)
                {
                    Console.WriteLine("SocketException: " + se.Message);
                }
            }
            Console.WriteLine("**ENDING IDENT**");
            listener.Close();
            IdentDT.Abort();
        }
    }
}
